<?php
class Forms_RaidTimeForm extends Zend_Dojo_Form 
{
	
	
		
	public function init()
	{
		$this->setName('raid_time');		
		
		$startDate = new Zend_Dojo_Form_Element_DateTextBox('start_date');
		$startDate->setLabel('Start Date')
		//->setDatePattern('Y-m-d')//http://www.unicode.org/reports/tr35/#Date_Format_Patterns
		->setAttrib("style","width: 85; height: 100%");
		
		$startTime = new Zend_Dojo_Form_Element_TimeTextBox('start_time');
		$startTime->setLabel('Start Time')
		->setAttrib("style","width: 85; height: 100%");
		
		$signupDate = new Zend_Dojo_Form_Element_DateTextBox('signup_date');
		$signupDate->setLabel('Signup Date')
		//->setDatePattern('Y-m-d')
		->setAttrib("style","width: 85; height: 100%");
		
		$signupTime = new Zend_Dojo_Form_Element_TimeTextBox('signup_time');
		$signupTime->setLabel('Signup Time')
		->setAttrib("style","width: 85; height: 100%");
		
		$finishDate = new Zend_Dojo_Form_Element_DateTextBox('finish_date');
		$finishDate->setLabel('Finish Date')
		//->setDatePattern('Y-m-d')
		->setAttrib("style","width: 85; height: 100%");
		
		$finishTime = new Zend_Dojo_Form_Element_TimeTextBox('finish_time');
		$finishTime->setLabel('Finish Time')
		->setAttrib("style","width: 85; height: 100%");
		
		$this->addElements(array($startDate, $startTime, $signupDate, $signupTime, $finishDate, $finishTime));
	}
	
	public function __construct($date = null)
	{
		parent::__construct();
		if ($date != null){
			$this->setDefault('start_date',date_format($date,'Y-m-d'));
			$this->calcDefaults($date);
			
		}
	}
	
	
	
	/**
	 * Requires that start date is set
	 *  - Sets the signup deadline at 8:00 pm the day before the start date
	 *  - Sets the start time to 8:00 pm if start day is a weekday and 5:00 pm if
	 * it is a sunday
	 *  - Sets the finish date to the same date as the start date and the finish time to
	 * 11:00 pm
	 *
	 */
	private function calcDefaults($date)
	{
		$signup = date_create(date_format($date,'Y-m-d'));
		date_modify($signup,'-1 day');
		
		$starttime;
		
		if (date_format($date,'D') != 'Sun') $starttime = 'T20:00:00';
		else $starttime = 'T17:00:00';
		
		
		
		$this->setDefaults(	array(
									'start_time' 	=> $starttime,
									'signup_date'	=> date_format($signup,'Y-m-d'),
									'signup_time'	=> 'T20:00:00',
									'finish_date'	=> date_format($date,'Y-m-d'),
									'finish_time'	=> 'T23:00:00'
							)
					);
		
	}
	
	
	
	
}
?>